package io.renren.modules.app.controller;
import io.renren.common.exception.RRException;
import io.renren.modules.app.entity.ResidentEntity;
import io.renren.modules.app.entity.Response;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.entity.UserWithCarNumber;
import io.renren.modules.app.service.ResidentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ZhuchangController
 * Package: io.renren.modules.app.controller
 * Description:
 *
 * @Author zzy
 * @Create 2024/5/22 17:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/app/zhuchang")
public class ZhuchangController {

    @Autowired
    private ResidentService residentService;

    @PostMapping("/update")
    public ResponseEntity<Response> updateResidentInfo(@RequestBody ResidentEntity newResident) {
        try {
            ResidentEntity residentEntity=residentService.updateResidentInfo(newResident);
            Response successResponse = new Response(0, "Updated successfully", residentEntity); // 或者你可以返回更新后的ResidentEntity
            return new ResponseEntity<>(successResponse, HttpStatus.OK);
        } catch (RRException e) {
            Response errorResponse = new Response(404, e.getMessage(), null); // 404可能不是最佳状态码，取决于你的具体需求
            return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping("/info")
    public ResponseEntity<Response> getUserInfo(@RequestParam("userId") int userId) {
        ResidentEntity resident = residentService.getUserById(userId);
        if (resident != null) {
            Response response = new Response(0, "success", resident);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } else {
            Response response = new Response(404, "User not found", null);
            return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
        }
    }
}